<?php

/**
 * @author liyi
 * @copyright 2018.12.13
 */

namespace Firegame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

class LoginController extends ApiController {
    
    const GAME_TYPE = 'Firegame';  // 游戏类型

    const DB_USER_NAME = 'fire_game.users'; // 游戏数据表

    const CACHE_FIRES_KEY = 'fires';  // 游戏全局缓存键

    const FIRE_TOURIST = "fires:tourist"; // 游戏游客用户

    const FIRE_AUTH_USER = "fires:auth_user";  // 授权用户

    const FIRE_USER_LOGIN = 'fires:user:%s';  // 玩家每日游戏签到缓存键

    // 登陆授权接口
    public function login()
    {
        $gameType = self::GAME_TYPE;
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');
        $id = I('id','','int');
        $openid = I('openid','','string');

        $data['openid'] = $openid;
        $data['nickname'] = $nickname;
        $data['avatar'] = $avatar;
        $data['id'] = $id;

        $dbname = self::DB_USER_NAME;
        $cacheKey = self::CACHE_FIRES_KEY;
        $cacheval = self::FIRE_AUTH_USER;

        $userInfo = $this->commLogin_g($dbname,$data,$cacheKey,$cacheval);

        $this->response(0,'ok',$userInfo);
    }

    //未授权登陆接口
    public function loginNotAuth() {
        $code = I('code','','string');
        $bs_code = I('type','','string');
        if(!$code) {
            $this->response(1,'code不能为空');
        }
        $gameType = self::GAME_TYPE;
        $dbname = self::DB_USER_NAME;
        $cacheKey = self::CACHE_FIRES_KEY;
        $cacheval = self::FIRE_TOURIST;
        //$userInfo = $this->commLoginNotAuth_g($gameType,$dbname,$code,$cacheKey,$cacheval);
        $userInfo = $this->commLoginNotAuth_g($gameType,$dbname,$code,$bs_code);

        $this->loginLog($userInfo['id']);
        if($userInfo['is_win'] <= 0) {
            $this->getUserList($userInfo['id']);
        }
    
        $this->response(0,'ok',$userInfo);

    }

    /**
     * 用户每日登陆记录
     */
    public function loginLog($uid) {

        $redis = REDIS('activity');
        $userKey = sprintf(self::FIRE_USER_LOGIN,$uid);  //用户主键分享到群

        $loginKey = $redis->get($userKey); // 获取用户签到缓存键
      
        if(empty($loginKey)) {

            $contion['id'] = $uid;
            $ret = M('users')->where($contion)->setInc('sign_num',1);

            $redis->set($userKey,1);  // 标记
            $redis->exprieAt($userKey,strtotime(date('Y-m-d 23:59:59'))); // 每日23点59分过期

            if(empty($ret)) {
                Log::write('修改用户每日登陆次数失败','WARN');
            }

        }
    }

    // 检测含有违法违规内容
    public function msgSecChecks() {
        $content = I('content','','string');  // 内容
        if(empty($content)) {
            $this->response(2,'请填写留言内容');
        }

        $redis = REDIS('activity');
        $userKey = 'fire:accessToken';
        $status = $redis->exists($userKey);
        if(empty($status)) {
            $gameType = self::GAME_TYPE;
            $data = $this->wxAccessToken($gameType); // 获取微信accessToken 值
            if(!empty($data['access_token']) && !empty($data['expires_in'])) {
                
                $cacheval = $data['access_token'];
                $expires_in = $data['expires_in'];
                $redis->set($userKey,$cacheval,$expires_in); // access 缓存键7200秒过期
            }
        }
        $cacheKey = $redis->get($userKey); // 获取全局accessToken键
        
        $ret = $this->msgSecCheck($cacheKey,$content);
      
        $this->response(0,'ok');  
    }

    // 获取仇人列表数据
    public function getUserList($uid) {
        $map['sign_num'] = array('egt',2); // 默认是2
        $map['id'] = array('neq',$uid);
        $list = M('users')->field('id')->where($map)->limit(50)->select();
 
        shuffle($list);
        $info = array_slice($list,0,5);
        
        $guide = array('交个朋友吧！手下败将！','来打赢我！把微信告诉我好吗？','打赢我！才有资格和我对话！','你不反击！怎么和我认识！','朋友！该升级了啊！');
   
        $insertData = array();
        $data = array('uid'=>$uid,'type'=>2,'is_win'=>2,'create_time'=>date('Y-m-d H:i:s'));
        foreach($info as $k=>$v) {
            $sortKey = array_rand($guide);
            $data['invite_uid'] = $v['id'];
            $data['guide'] = $guide[$sortKey];
            $insertData[] = $data;
        }
        $ret = M('play_log')->addAll($insertData);
        $where['id'] = $uid;
        M('users')->where($map)->save(array('is_win'=>5));
    }
       
}